/* File: Save.m - (Interactive) Unix shell version of SavePanel
 *
 * By: Christopher Lane (lane@sumex-aim.stanford.edu)
 *
 * Date: 10 November 1992
 *
 * Copyright: 1990, 1991 & 1992 by The Leland Stanford Junior University.
 */

#import <stdlib.h>
#import <getopt.h>
#import <appkit/SavePanel.h>
#import <appkit/Application.h>

#define USAGE "usage: %s [-f file] [-d directory] [-t type] [-p prompt] [-T title] [-u]\n"
#define EXIT_USAGE (2)

void main(int argc, char *argv[])
{
	SavePanel *panel;
	const char *name = NULL, *path = NULL;
	int option, status = EXIT_SUCCESS;
	
	NXApp = [Application new];
	panel = [SavePanel new];
	
	while ((option = getopt(argc, argv, "f:d:t:p:T:u")) != EOF)
		switch (option) {
		case 'f': name = optarg; break;
		case 'd': path = optarg; break;
		case 't': [panel setRequiredFileType:optarg]; break;
		case 'p': [panel setPrompt:optarg]; break;
		case 'T': [panel setTitle:optarg]; break;
		case 'u': [panel setTreatsFilePackagesAsDirectories:YES]; break;
		default : status = EXIT_USAGE;
		}
	if (optind < argc) status = EXIT_USAGE;

	if (status == EXIT_USAGE) (void) fprintf(stderr, USAGE, [NXApp appName]);
	else {
		int context = [NXApp activateSelf:YES];
		
		if ([panel runModalForDirectory:path file:name]) (void) puts([panel filename]);
		else status = EXIT_FAILURE;
		
		if (context) (void) [NXApp activate:context];
		}
	
	[panel free];

	[NXApp free];
	
	exit(status);
}
